namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class reviews
    {
        // Methods
        public static void Add(review model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into reviews(");
            builder.Append("[categoryid],[pid],[username],[sex],[qq],[email],[ip],[time],[face],[body],[replybody],[replytime],[replyer],[approved],[visible]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.categoryid + ",");
            builder.Append("" + model.pid + ",");
            builder.Append("'" + model.username + "',");
            builder.Append("" + (model.sex ? 1 : 0) + ",");
            builder.Append("'" + model.qq + "',");
            builder.Append("'" + model.email + "',");
            builder.Append("'" + model.ip + "',");
            builder.Append("'" + model.time + "',");
            builder.Append("'" + model.face + "',");
            builder.Append("'" + model.body + "',");
            builder.Append("'" + model.replybody + "',");
            builder.Append("'" + model.replytime + "',");
            builder.Append("'" + model.replyer + "',");
            builder.Append("" + (model.approved ? 1 : 0) + ",");
            builder.Append("" + (model.visible ? 1 : 0) + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete reviews ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from reviews where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from reviews where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int)scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from reviews ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from reviews where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from reviews where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static review GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from reviews ");
            builder.Append(" where id=" + id);
            review review = new review();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            review.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["categoryid"].ToString() != "")
            {
                review.categoryid = int.Parse(dataTable.Rows[0]["categoryid"].ToString());
            }
            if (dataTable.Rows[0]["pid"].ToString() != "")
            {
                review.pid = int.Parse(dataTable.Rows[0]["pid"].ToString());
            }
            review.username = dataTable.Rows[0]["username"].ToString();
            if (dataTable.Rows[0]["sex"].ToString() != "")
            {
                if ((dataTable.Rows[0]["sex"].ToString() == "1") || (dataTable.Rows[0]["sex"].ToString().ToLower() == "true"))
                {
                    review.sex = true;
                }
                else
                {
                    review.sex = false;
                }
            }
            review.qq = dataTable.Rows[0]["qq"].ToString();
            review.email = dataTable.Rows[0]["email"].ToString();
            review.ip = dataTable.Rows[0]["ip"].ToString();
            if (dataTable.Rows[0]["time"].ToString() != "")
            {
                review.time = DateTime.Parse(dataTable.Rows[0]["time"].ToString());
            }
            review.face = dataTable.Rows[0]["face"].ToString();
            review.body = dataTable.Rows[0]["body"].ToString();
            review.replybody = dataTable.Rows[0]["replybody"].ToString();
            if (dataTable.Rows[0]["replytime"].ToString() != "")
            {
                review.replytime = DateTime.Parse(dataTable.Rows[0]["replytime"].ToString());
            }
            review.replyer = dataTable.Rows[0]["replyer"].ToString();
            if (dataTable.Rows[0]["approved"].ToString() != "")
            {
                if ((dataTable.Rows[0]["approved"].ToString() == "1") || (dataTable.Rows[0]["approved"].ToString().ToLower() == "true"))
                {
                    review.approved = true;
                }
                else
                {
                    review.approved = false;
                }
            }
            if (dataTable.Rows[0]["visible"].ToString() != "")
            {
                if ((dataTable.Rows[0]["visible"].ToString() == "1") || (dataTable.Rows[0]["visible"].ToString().ToLower() == "true"))
                {
                    review.visible = true;
                    return review;
                }
                review.visible = false;
            }
            return review;
        }

        public static void Update(review model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update reviews set ");
            builder.Append("[categoryid]=" + model.categoryid + ",");
            builder.Append("[pid]=" + model.pid + ",");
            builder.Append("[username]='" + model.username + "',");
            builder.Append("[sex]=" + (model.sex ? 1 : 0) + ",");
            builder.Append("[qq]='" + model.qq + "',");
            builder.Append("[email]='" + model.email + "',");
            builder.Append("[ip]='" + model.ip + "',");
            builder.Append("[time]='" + model.time + "',");
            builder.Append("[face]='" + model.face + "',");
            builder.Append("[body]='" + model.body + "',");
            builder.Append("[replybody]='" + model.replybody + "',");
            builder.Append("[replytime]='" + model.replytime + "',");
            builder.Append("[replyer]='" + model.replyer + "',");
            builder.Append("[approved]=" + (model.approved ? 1 : 0) + ",");
            builder.Append("[visible]=" + (model.visible ? 1 : 0) + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }

 

}

